{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 算法第一题\n",
    "写一个函数输入：两个中文字符串，判断字符串是否具有相同的规律\n",
    "\n",
    "示例 1: 输入: string1 = \"我高高兴兴去钓鱼\", string2 = \"他快快乐乐去上班\" 输出: true\n",
    "\n",
    "示例 2: 输入:string1 = \"红彤彤的太阳\", string2 = \"蓝茵茵的湖水\" 输出: true\n",
    "\n",
    "示例 3: 输入: string1 = \"胖乎乎的手\", string2 = \"红扑扑的脸\" 输出: true\n",
    "\n",
    "示例 4: 输入: string1 = \"闪闪的星星\", string2 = \"明亮的眼睛\" 输出: false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-25T10:46:56.447608Z",
     "start_time": "2022-01-25T10:46:56.421608Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['我', '高', '高', '兴', '兴', '去', '钓', '鱼']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'我': 0, '高': 2, '兴': 4, '去': 5, '钓': 6, '鱼': 7}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'02244567'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'02244567'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "string1 = \"我高高兴兴去钓鱼\"\n",
    "string1_li = list(string1)\n",
    "string1_li\n",
    "\n",
    "string1_dict = {k:v for v,k in enumerate(string1_li)}\n",
    "string1_dict\n",
    "new_string1_li = [string1_dict[x] for x in string1_li]\n",
    "\n",
    "new_string1 = ''.join([str(x) for x in new_string1_li])\n",
    "new_string1\n",
    "\n",
    "string2 = \"他快快乐乐去上班\"\n",
    "string2_li = list(string2)\n",
    "string2_dict = {k:v for v,k in enumerate(string2_li)}\n",
    "new_string2_li = [string2_dict[x] for x in string2_li]\n",
    "new_string2 = ''.join([str(x) for x in new_string2_li])\n",
    "new_string2\n",
    "\n",
    "new_string1 == new_string2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-25T11:07:48.617510Z",
     "start_time": "2022-01-25T11:07:48.591510Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_new_string(string:str)->bool:\n",
    "    string1_li = list(string)\n",
    "    string1_dict = {}\n",
    "    for index,item in enumerate(string1_li):\n",
    "        if item not in string1_dict.keys():\n",
    "            string1_dict[item] = index\n",
    "    new_string1 = ''.join([str(string1_dict[x]) for x in string1_li])\n",
    "    return new_string1\n",
    "\n",
    "\n",
    "def same_text(string1:str,string2:str)->bool:\n",
    "    '''\n",
    "    比较两个字符串的句式是否相同。\n",
    "    '''\n",
    "    new_string1 = get_new_string(string1)\n",
    "    new_string2 = get_new_string(string2)\n",
    "    if new_string1 == new_string2:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "same_text(\"我高高兴兴去钓鱼\",\"他快快乐乐去上班\")\n",
    "same_text(\"红彤彤的太阳\",\"蓝茵茵的湖水\")\n",
    "same_text(\"胖乎乎的手\",\"红扑扑的脸\")\n",
    "same_text(\"闪闪的星星\",\"明亮的眼睛\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-25T10:54:22.165909Z",
     "start_time": "2022-01-25T10:54:22.155909Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A_1': 11, 'B_1': 12, 'C_1': 13}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表推导式\n",
    "li = [1,2,3,4,5]\n",
    "li1 = [str(x) for x in li]\n",
    "\n",
    "# 字典推导式\n",
    "dict1 = {'A':1, 'B':2, 'C':3}\n",
    "dict2 = {k+'_1':v+10 for k,v in dict1.items()}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
